using System.Drawing;
using System.Xml.Schema;
using System.Xml.Serialization;
using flickr.api.communication;

namespace flickr.api
{
    public enum PhotoUrl
    {
        Small,
        Thumbnail,
        Square,
        Meduim,
        Large,
        Original
    }

	public class Photo : FlickrObject
	{
	    public Photo():base(){}
	    public Photo(FlickrConnection connection) : base(connection){}
	    
        private Bitmap _image = null;

        [XmlAttribute("id", Form=XmlSchemaForm.Unqualified)]
        public string PhotoId;
    
        [XmlAttribute("owner", Form=XmlSchemaForm.Unqualified)]
        public string UserId;
    
        [XmlAttribute("secret", Form=XmlSchemaForm.Unqualified)]
        public string Secret;
    
        [XmlAttribute("server", Form=XmlSchemaForm.Unqualified)]
        public string Server;
    
        [XmlAttribute("title", Form=XmlSchemaForm.Unqualified)]
        public string Title;
    
        [XmlAttribute("ispublic", Form=XmlSchemaForm.Unqualified)]
        public int IsPublic;
    
        [XmlAttribute("isfriend", Form=XmlSchemaForm.Unqualified)]
        public int IsFriend;
    
        [XmlAttribute("isfamily", Form=XmlSchemaForm.Unqualified)]
        public int IsFamily;

        [XmlAttribute("isprimary", Form=XmlSchemaForm.Unqualified)]
        public int IsPrimary;

        [XmlAttribute("license", Form=XmlSchemaForm.Unqualified)]
        public string License;

        [XmlAttribute("dateuploaded", Form=XmlSchemaForm.Unqualified)]
        public string DateUploaded;

        [XmlAttribute("datetaken", Form=XmlSchemaForm.Unqualified)]
        public string DateTaken;

        [XmlAttribute("ownername", Form=XmlSchemaForm.Unqualified)]
        public string OwnerName;

        [XmlAttribute("iconserver", Form=XmlSchemaForm.Unqualified)]
        public string IconServer;

        private const string photoUrl = "http://static.flickr.com/{0}/{1}_{2}{3}.jpg";

        [XmlIgnore]
        public string WebUrl
        {
            get { return string.Format("http://www.flickr.com/photos/{0}/{1}/", UserId, PhotoId); }
        }

        [XmlIgnore]
        public string SquareThumbnailUrl
        {
            get { return string.Format(photoUrl, Server, PhotoId, Secret, "_s"); }
        }

        [XmlIgnore]
	    public Image SquareThumbnail
	    {
            get { return DownloadImage(SquareThumbnailUrl); }
	    }

        [XmlIgnore]
        public string ThumbnailUrl
        {
            get { return string.Format(photoUrl, Server, PhotoId, Secret, "_t"); }
        }

        [XmlIgnore]
	    public Image Thumbnail
	    {
            get { return DownloadImage(ThumbnailUrl); }
	    }

        [XmlIgnore]
        public string SmallUrl
        {
            get { return string.Format(photoUrl, Server, PhotoId, Secret, "_m"); }
        }

        [XmlIgnore]
	    public Image SmallImage
	    {
            get { return DownloadImage(SmallUrl); }
	    }

        [XmlIgnore]
        public string MediumUrl
        {
            get { return string.Format(photoUrl, Server, PhotoId, Secret, ""); }
        }

        [XmlIgnore]
	    public Image MediumImage
	    {
            get { return DownloadImage(MediumUrl); }
	    }

        [XmlIgnore]
        public string LargeUrl
        {
            get { return string.Format(photoUrl, Server, PhotoId, Secret, "_b"); }
        }

        [XmlIgnore]
	    public Image LargeImage
	    {
            get { return DownloadImage(LargeUrl); }
	    }
	    
        [XmlIgnore]
        public string OriginalUrl
        {
            get { return string.Format(photoUrl, Server, PhotoId, Secret, "_o"); }
        }

        [XmlIgnore]
	    public Image OriginalImage
	    {
            get { return DownloadImage(OriginalUrl); }
	    }

//	    public ExifPhoto ExifTags
//	    {
//            get { return Flickr.Photos.getExif(PhotoId, Secret); }
//	    }
	    public Sizes AvailableSizes
	    {
            get { return Flickr.Photos.getSizes(PhotoId); }
	    }
        private Image DownloadImage(string url)
        {
            return Connection.DownloadImageFromUrl(url);
		    //_image.Save(FileName, ImageFormat.Jpeg);
        }

        [XmlIgnore]
	    public string FileName
	    {
	        get
	        {
                return PhotoId + "_" + Secret;
//                string fileNameFormat = "{0}\\{1}.{2}";
//                string photoFolder = Preferences.Instance.PhotoFolder;
//                return string.Format(fileNameFormat, photoFolder, Title, ImageFormat.Bmp.ToString());
	        }
	    }

        [XmlIgnore]
	    public bool IsDownloaded
	    {
	        get { return _image != null; }
	    }

        private PhotoInfo _info = null;
	    public PhotoInfo Info
	    {
            get 
            {
                if (_info == null)
                {
                    _info = Flickr.Photos.getInfo(PhotoId, Secret);
                }
                return _info;  
            }
	    }
//        public FlickrNet.Size[] Sizes
//        {
//            get
//            {
//                return new Connectr().Flickr.PhotosGetSizes(_flickrPhoto.PhotoId).SizeCollection;
//            }
//        }

//        public int Width
//        {
//            get
//            {
//                return LoadImageFromFile(FileName).Width;
//            }
//        }
//
//        public int Height
//        {
//            get 
//            {
//                return LoadImageFromFile(FileName).Height;
//            }
//        }
//
//        private Bitmap LoadImageFromFile(string fileName)
//        {
//            if (_image == null) 
//            {
//                _image = (Bitmap)Bitmap.FromFile(fileName);
//            }
//            return _image;
//        }

	}
}
